package com.zscat.mallplus.dms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.douyin.open.api.ToutiaoClientTokenApi;
import com.douyin.open.models.ToutiaoOauth2Oauth2InlineResponse2002;
import com.jpay.util.StringUtils;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IQsgStoreService;
import com.zscat.mallplus.enums.OrderStatus;
import com.zscat.mallplus.qsg.QsgStore;
import com.zscat.mallplus.utils.DateUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


public class OrderStatusTask extends QuartzJobBean {
    @Autowired
    private IDmsOrderService orderService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        //3分钟未付款或付款失败则失效
        List<Integer> statues = new ArrayList<>();
        statues.add(OrderStatus.PayNotNotice.getValue());
        statues.add(OrderStatus.INIT.getValue());
        QueryWrapper qw = new QueryWrapper();
        qw.in("order_status",statues);
        List<DmsOrder> orders = orderService.list(qw);
        Long currentTime = System.currentTimeMillis();
        if (null!=orders&&orders.size()>0){
            for (DmsOrder order : orders){
                if ((currentTime - order.getCreateDatetime().getTime())>120*1000){
                    order.setOrderStatus(OrderStatus.CLOSED.getValue());
                    orderService.updateById(order);
                }
            }
        }
    }
}
